package com.wlw.admin.openeye.mvp.presenter

import com.wlw.admin.openeye.base.BasePresenter
import com.wlw.admin.openeye.mvp.contract.CategoryDetailContract
import com.wlw.admin.openeye.mvp.model.CategoryDetailModel

class CategoryDetailPresenter : BasePresenter<CategoryDetailContract.View>(), CategoryDetailContract.Presenter {
    private val categoryDetailModel by lazy {
        CategoryDetailModel()
    }
    private var nextPageUrl: String? = null

    override fun getCategoryDetailList(id: Long) {
        checkViewAttached()
        val disposable = categoryDetailModel.getCategoryDetailList(id)
                .subscribe({
                    mRootView?.apply {
                        nextPageUrl = it.nextPageUrl
                        setCateDetailList(it.itemList)
                    }
                }, { t ->
                    mRootView?.apply {
                        showError(t.toString())
                    }
                })
        addSubscription(disposable)
    }

    override fun loadMoreData() {
        val disposable = nextPageUrl?.let {
            categoryDetailModel.loadMoreData(it)
                    .subscribe({ issue ->
                        mRootView?.apply {

                            nextPageUrl = issue.nextPageUrl
                            setCateDetailList(issue.itemList)
                        }

                    }, { t ->
                        mRootView?.apply {
                            showError(t.toString())
                        }
                    })
        }
        disposable?.let { addSubscription(it) }
    }

}